package com.futao.niudong.controller;

import com.alibaba.fastjson.JSON;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.MultiGetRequest;
import org.elasticsearch.action.get.MultiGetRequestBuilder;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.UUID;

/**
 * @author futao@gmail.com
 * @date 2022/3/10
 */
@RestController
@RequestMapping("/es/bulk")
public class EsBulkController {

    public static final String LDK_INDEX = "ldk_index";
    public static final String LDK_TYPE = "ldk_type";

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @GetMapping("/mget")
    public String multiGet() throws IOException {
        MultiGetRequest multiGetRequest = new MultiGetRequest();
        multiGetRequest.add(new MultiGetRequest.Item(LDK_INDEX, "ddffc4b7-8d6e-4198-ad7f-27e063939dfe"))
                .add(new MultiGetRequest.Item(LDK_INDEX, "ddffc4b7-8d6e-4198-ad7f-27e063939dfe"));
        MultiGetResponse multiGetResponse = restHighLevelClient.mget(multiGetRequest, RequestOptions.DEFAULT);
        return JSON.toJSONString(multiGetResponse, true);
    }

    @PostMapping("/bulk/ops")
    public String bulkOps() throws IOException {
        BulkRequest bulkRequest = new BulkRequest();
        IndexRequest indexRequest1 = new IndexRequest(LDK_INDEX);
        EsController.UserObject userObject1 = new EsController.UserObject(UUID.randomUUID().toString(), "李达康", 18, true, new String[]{"抽烟", "喝酒", "烫头"});
        System.out.println(userObject1.getId());
        indexRequest1.id(userObject1.getId())
                .source(JSON.toJSONString(userObject1, true), XContentType.JSON);

        EsController.UserObject userObject2 = new EsController.UserObject(UUID.randomUUID().toString(), "李达康", 18, true, new String[]{"抽烟", "喝酒", "烫头"});
        IndexRequest indexRequest2 = new IndexRequest(LDK_INDEX);
        System.out.println(userObject2.getId());
        indexRequest2.id(userObject2.getId())
                .source(JSON.toJSONString(userObject2, true), XContentType.JSON);

        bulkRequest.add(indexRequest1)
                .add(indexRequest2)
        // .add(new UpdateRequest())
        // .add(new DeleteRequest())
        ;
        BulkResponse bulkResponse = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        return JSON.toJSONString(bulkResponse, true);
    }
}
